package com.smartkey.adapter;

import java.util.ArrayList;

import com.project.smartkey.R;
import com.smartkey.bean.AppInfo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AppListAdapter extends BaseAdapter{
	private ArrayList<AppInfo> appList;
	private Context context;
	private LayoutInflater inflater;
	public AppListAdapter(Context ctx, ArrayList<AppInfo> list) {
		this.appList = list;
		this.context = ctx;
		inflater = LayoutInflater.from(context);
	}
	
	public void resetData(ArrayList<AppInfo> list) {
		this.appList = list;
	}
	
	@Override
	public int getCount() {
		return appList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int arg0, View convertView, ViewGroup arg2) {
		AppInfo appInfo = appList.get(arg0);
		
		Holder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.app_list_item,
					null);
			holder = getHolder(convertView);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		if(appInfo!=null){
			if(appInfo.appIcon!=null){
				holder.icon.setBackgroundDrawable(appInfo.appIcon);
			}
			if(appInfo.appName!=null){
				holder.name.setText(appInfo.appName);
			}
		}
		
		
		return convertView;
	}
	
	private Holder getHolder(View convertView) {
		Holder holder = new Holder();
		holder.icon = (ImageView) convertView
				.findViewById(R.id.app_icon);
		holder.name = (TextView) convertView
				.findViewById(R.id.app_name);
		return holder;
	}

	private class Holder {
		ImageView icon;
		TextView name;
	}

}
